//
//  NSDate+MJ.m
//  ItcastWeibo
//
//  Created by apple on 14-5-9.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "NSDate+MJ.h"

@implementation NSDate (MJ)
/**
 *  是否为今天
 */
- (BOOL)isToday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

/**
 *  是否为昨天
 */
- (BOOL)isYesterday
{
    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD];
    
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    //用这个方法更好
//    [calendar isDateInYesterday:<#(nonnull NSDate *)#>]
    
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 1;
}

- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}

/**
 *  是否为今年
 */
- (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitYear;
    
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return nowCmps.year == selfCmps.year;
}

- (NSDateComponents *)deltaWithNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}


+(NSString*)nowTime{
    NSDate *nowDate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString *  locationString=[dateformatter stringFromDate:nowDate];
    
    return locationString;

}




+(NSDate*)convertDateWithString:(NSString*)timeStr{

    // 1.获得微博的发送时间
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    //#warning 真机调试下, 必须加上这段
//    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [fmt setLocale:[NSLocale currentLocale]];
//        [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] ];
    
    NSDate *createdDate = [fmt dateFromString:timeStr];

    
    return createdDate;

}


- (BOOL)isBetweenFromDate:(NSDate*)fromDate toDate:(NSDate*)toDate{



   int i = [NSDate compareOneDay:self withAnotherDay:fromDate];
   int j = [NSDate compareOneDay:toDate withAnotherDay:self];
    
    if (i == 1 && j == 1) {
        return YES;
    }
    else{
    
        return NO;
    }
}


-(NSString *)getDayWeek:(int)dayDelay withComponent:(YWDateComponentsStyle)dateCompoment{
    NSString *weekDay;
    NSDate *dateNow;
    
    //1.时间
    dateNow = [NSDate dateWithTimeIntervalSinceNow:dayDelay*24*60*60];//dayDelay代表向后推几天，如果是0则代表是今天，如果是1就代表向后推24小时
    //2.日历
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//设置成中国阳历
    //3.元素
    NSDateComponents *comps = [[NSDateComponents alloc] init];
   NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    comps =[calendar components:unitFlags fromDate:dateNow];
    
    long weekday = [comps weekday]; //获取星期对应的长整形字符串
    long day=[comps day];//获取日期对应的长整形字符串
    long year=[comps year];//获取年对应的长整形字符串
    long month=[comps month];//获取月对应的长整形字符串
    long hour=[comps hour];//获取小时对应的长整形字符串
    long minute=[comps minute];//获取月对应的长整形字符串
    long second=[comps second];//获取秒对应的长整形字符串
    
    
    NSString *returnString;
   
    switch (weekday) {
        case 1:
            weekDay=@"星期日";
            break;
        case 2:
            weekDay=@"星期一";
            break;
        case 3:
            weekDay=@"星期二";
            break;
        case 4:
            weekDay=@"星期三";
            break;
        case 5:
            weekDay=@"星期四";
            break;
        case 6:
            weekDay=@"星期五";
            break;
        case 7:
            weekDay=@"星期六";
            break;
            
        default:
            break;
    }
    
    switch (dateCompoment) {
        case YWYearCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",year];
            
        }
            break;
        case YWMonthCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",month];
            
        }
            break;
        case YWDayCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",day];
            
        }
            break;
        case YWWeekdayCalendarUnit:
        {
            returnString = weekDay;
            
        }
            break;
        case YWHourCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",hour];
            
        }
            break;
        case YWMinuteCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",minute];
            
        }
            break;
        case YWSecondCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",second];
            
        }
            break;

        //用作headerID用
        case YWYearAndMonthCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld%ld",year,month];
            
        }
            break;
        //用作ID用
        case YWDateAndTimeCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",year,month,day,hour,minute,second];
            
        }
            break;
      
            
        default:
            break;
    }
    
    
    
    return returnString;
}



-(NSString *)getDayWithComponent:(YWDateComponentsStyle)dateCompoment{
    NSString *weekDay;
    NSDate *dateNow;
    
    //1.时间
    dateNow = self;//具体某一天，所以时间就是自己
    
    //2.日历
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//设置成中国阳历
    //3.元素
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    comps =[calendar components:unitFlags fromDate:dateNow];
    
    long weekday = [comps weekday]; //获取星期对应的长整形字符串
    long day=[comps day];//获取日期对应的长整形字符串
    long year=[comps year];//获取年对应的长整形字符串
    long month=[comps month];//获取月对应的长整形字符串
    long hour=[comps hour];//获取小时对应的长整形字符串
    long minute=[comps minute];//获取月对应的长整形字符串
    long second=[comps second];//获取秒对应的长整形字符串
    
    
    NSString *returnString;
    
    switch (weekday) {
        case 1:
            weekDay=@"星期日";
            break;
        case 2:
            weekDay=@"星期一";
            break;
        case 3:
            weekDay=@"星期二";
            break;
        case 4:
            weekDay=@"星期三";
            break;
        case 5:
            weekDay=@"星期四";
            break;
        case 6:
            weekDay=@"星期五";
            break;
        case 7:
            weekDay=@"星期六";
            break;
            
        default:
            break;
    }
    
    switch (dateCompoment) {
        case YWYearCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",year];
            
        }
            break;
        case YWMonthCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",month];
            
        }
            break;
        case YWDayCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",day];
            
        }
            break;
        case YWWeekdayCalendarUnit:
        {
            returnString = weekDay;
            
        }
            break;
        case YWHourCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",hour];
            
        }
            break;
        case YWMinuteCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",minute];
            
        }
            break;
        case YWSecondCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld",second];
            
        }
            break;
            
            //用作headerID用
        case YWYearAndMonthCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld%ld",year,month];
            
        }
            break;
            //用作ID用
        case YWDateAndTimeCalendarUnit:
        {
            returnString =[NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",year,month,day,hour,minute,second];
            
        }
            break;
            
            
        default:
            break;
    }
    
    return returnString;

}


+ (int)compareOneCalendarDay:(NSDate *)oneDay withAnotherCalendarDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
//    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}

//传入今天的时间，返回明天的时间
+(NSString *)GetTomorrowDay:(NSDate *)aDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+0)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}

@end
